package ai.zixing.mashibing.basic_class.class04;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Code01_Comparator {

    public static class Deal{
        public int id;
        public int one;
        public int two;
        public int three;
        public int four;

        public Deal(int id, int one, int two, int three, int four) {
            this.id = id;
            this.one = one;
            this.two = two;
            this.three = three;
            this.four = four;
        }

        @Override
        public String toString() {
            return "Deal{" +
                    "id=" + id +
                    ", one=" + one +
                    ", two=" + two +
                    ", three=" + three +
                    ", four=" + four +
                    '}';
        }
    }

    // 按照one降序
    // one 相同则 two 降序
    // two 相同则 three 升序
    // three 相同则 four 降序
    public static class SortComparator implements Comparator<Deal> {
        @Override
        public int compare(Deal o1, Deal o2) {
            if (o1.one != o2.one) {
                // one 不同则降序
                return o2.one - o1.one;
            } else if (o1.two != o2.two) {
                return o2.two - o1.two;
            } else if (o1.three != o2.three) {
                return o1.three - o2.three;
            } else {
                return o2.four -o1.four;
            }
        }
    }


    public static void main(String[] args) {
        int max = 2;
        int dealNum = 8;
        Deal[] list = new Deal[dealNum];
        for (int i = 0; i < dealNum; i++) {
            int one = (int) (Math.random() * max);
            int two = (int) (Math.random() * max);
            int three = (int) (Math.random() * max);
            int four = (int) (Math.random() * max);
            list[i] = (new Deal(i + 1, one, two, three, four));
        }
        for (Deal deal : list) {
            System.out.println(deal);
        }
        Arrays.sort(list, new SortComparator());
        System.out.println("===================");
        for (Deal deal : list) {
            System.out.println(deal);
        }
    }
}
